package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMingXiShouLi;
import com.vf.admin.miaodong.entity.AppMingXiZuanShi;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppMingXiZuanShiDao.java
 * @Description TODO
 * @createTime 2021年11月02日 14:39:00
 */
@Repository
public class AppMingXiZuanShiDao extends BaseDao<AppMingXiZuanShi> {

    //字段列表
    public final String FIELDS = "`id`, `userid`, `pic`, `nick`, `phone`, `num`, `leixing`, `beizhu`, `createtime`";

    public void saveOrUpdate(AppMingXiZuanShi appMingXiZuanShi) {
        if (appMingXiZuanShi.getId() == null || appMingXiZuanShi.getId() == 0) {
            appMingXiZuanShi.setId(this.add(appMingXiZuanShi));
        } else {
            this.update(appMingXiZuanShi);
        }
    }

    public List<AppMingXiZuanShi> findList(Integer userid, SplitPageBean splitPageBean) {
        StringBuilder str = new StringBuilder("");
        str.append("select " + FIELDS + " from app_mingxi_zuanshi where 1=1 ");
        List<Object> list = new ArrayList<>();
        str.append(" and userid = ? ");
        list.add(userid);
        str.append(" order by createtime desc ");
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiZuanShi.class, splitPageBean);
    }


    /**
     * 更新用户头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateImg(Integer userid,String pic){
        if(userid==null || userid<=0 || StringEx.isNull(pic)){
            return ;
        }
        String sql = " UPDATE app_mingxi_zuanshi SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新用户昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateNick(Integer userid,String nick){
        if(userid==null || userid<=0 || StringEx.isNull(nick)){
            return ;
        }
        String sql = " UPDATE app_mingxi_zuanshi SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    public List<AppMingXiZuanShi> listAfter(Map<String, Object> params) {

        List<Object> paramList = new ArrayList<>();

        StringBuilder str = new StringBuilder();

        str.append("select * from app_mingxi_zuanshi where 1=1 ");
        //锁定用户
        if(params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))){
            str.append(" and userid in ( ");
            str.append(params.get("userids"));
            str.append(" ) ");
        }
        if(params.containsKey("leixing") && StringEx.isNotNull(params.get("leixing"))) {
            str.append(" and leixing = ? " );
            paramList.add(params.get("leixing"));
        }

        //
        if(params.containsKey("today") && StringEx.isNotNull(params.get("today"))) {
            str.append(" and createtime like ? " );
            paramList.add("%" + params.get("today") + "%");
        }
        if(params.containsKey("alltoday") && StringEx.isNotNull(params.get("alltoday"))) {
            str.append(" and createtime <= ? " );
            paramList.add(params.get("alltoday"));
        }

        String sql = str.toString();
        System.out.println("最终"+sql);
        return this.queryList(sql, paramList, AppMingXiZuanShi.class);
    }

}
